/* @(#)GSLoginAction.java    
*
* Project: BUS
*
* Modify Information:
* =============================================================================
*   Author       Date       Description
*   ------------ ---------- ---------------------------------------------------
*   Simonyi Peng 2006-2-7    建立
*
* Copyright Notice:
* =============================================================================
*    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
*        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
*        Haidian District, Beijing ,100081, China All rights reserved.
*
*    This software is the confidential and proprietary information of
*        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
*        You shall not disclose such Confidential Information and shall use it
*        only in accordance with the terms of the license agreement you entered
*        into with HiTRUST.
*
* Warning:
* =============================================================================
* 
*/
package com.hitrust.gs.action;

import java.util.ArrayList;

import system.LoginContext;

import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.business.GSOrgProcessor;
import com.hitrust.gs.business.GSOrderProcesser;
import com.hitrust.gs.business.GSOrderProcessor;
import com.hitrust.gs.business.GSUniteGwProcessor;
import com.hitrust.gs.form.GSOrderModifyQueryForm;

import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.exception.*;
import com.hitrust.gs.util.page.ResultProcess;
import com.hitrust.gs.vo.GSOrderModifyQueryVO;


public class GSOrderModifyQueryAction extends GSBaseAction {
	public String execute(GSBaseForm aForm) throws GSException {
	    
	    LoginContext context = (LoginContext)aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
	    GSUniteGwProcessor unitepro=new GSUniteGwProcessor();
        GSOrderProcesser ordProcesser = new GSOrderProcesser();


	    GSOrderModifyQueryForm tForm = (GSOrderModifyQueryForm)aForm;
	    GSOrderModifyQueryVO omqVO = new GSOrderModifyQueryVO();
	    omqVO.orderId = tForm.getOrderId();
	    omqVO.deptId = tForm.getOrderDept();
	    omqVO.concentricityId = tForm.getConcentricityId();
	    omqVO.manufInfo = tForm.getManufInfo();
	    omqVO.beginDate = tForm.getOrderCreateDateStr();
	    omqVO.endDate = tForm.getOrderCreateDateEnd();
	    omqVO.detailStatus = tForm.getDetailStatus();
	    omqVO.drawingpiececode = tForm.getDrawingpiececode();
	    
	    if(aForm.getRequest().getAttribute("ISQUERYCONDITIONEXIST")!=null){
	        //是执行完返回的，读出查询条件
	        omqVO = (GSOrderModifyQueryVO)aForm.getSession().getAttribute("MODIFYQUERYCONDITION");
	    }
	    else{
		    //初次查询，缓存查询条件
		    aForm.getSession().setAttribute("MODIFYQUERYCONDITION",omqVO);
	    }

        try{
            String[] tStr = this.getPageParams(tForm,omqVO);
		    ArrayList orderAry = ordProcesser.getModifyNeedOrder(omqVO,Integer.parseInt(tStr[0]),Integer.parseInt(tStr[1]));
	    	    
            ResultProcess tRsProc = new ResultProcess();
            tRsProc.setResult(orderAry);
            tForm.getRequest().setAttribute("orderlist",tRsProc);
            //存储分页参数
            tForm.getRequest().setAttribute("pageSize",tStr[1]);
            tForm.getRequest().setAttribute("totalRecords",tStr[2]);
            
            ArrayList tList = unitepro.getWareHouseLevelName(context.getLoginWarehouseCode(), context.getLoginWarehouseCode());
		    tForm.getRequest().setAttribute("orderdept", tList);  
            
        }catch(GSException e){
            aForm.getRequest().setAttribute("action", "A100501.do");
            throw e;
        }catch(Exception ex){
            GSLogWriter.error(this,ex.toString());
            tForm.getRequest().setAttribute("action","A100501.do");
            throw new GSException(GSException.E10030,GSException.E10030_MSG);
        }
        
        return GSBaseConstants.FORWARD_SUCCESS;
        
	}
	
	   /**
	    * 取得分页参数
	    * @param aForm         基类form
	    * @param aOrderVO      订单VO
	    * @param aOrg          组织ArrayList对象
	    * @return
	    * @throws Exception
	    */
	   private String[] getPageParams(GSBaseForm aForm,GSOrderModifyQueryVO tVO) throws Exception{
	       String tStr[] = new String[3];
	       int totalRecords;
	       int pageNo;
	       int pageSize;
	       String tPage = GSBaseConstants.tPage;
	       GSOrderProcesser ordProcesser = new GSOrderProcesser();
	       //根据页面传入的查询参数查询订单信息
	       if(aForm.getChoice() == null || aForm.getChoice().equals("")){
               totalRecords = ordProcesser.getModifyNeedOrderCount(tVO);
	           
	           // 取得系统默认的页大小
	           pageSize = Integer.parseInt(tPage);
	           // PageNo=1
	           pageNo = 1;
	           int n = (totalRecords % pageSize == 0 )? totalRecords / pageSize:totalRecords / pageSize + 1;
	           if (pageNo>n) {
	               pageNo=n;
	           }
	           tStr[0] = "" + pageNo;
	           tStr[1] = "" + pageSize;
	           tStr[2] = "" + totalRecords;
	       }else{
	           tStr = GSBaseManager.getPageNoAndPageSize(aForm);
	       }
	       return tStr;
	   }   

}
